#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-11-30
# software: PyCharm

import time


def time_cost(func):

    def wrapper(*args, **kwargs):

        start_time = time.time()

        result = func(*args, **kwargs)

        end_time = time.time()
        print(f"{func.__name__} 函数运行耗时: {end_time - start_time} 秒")
        return result
    return wrapper



@time_cost
def print_prime_numbers():
    """
    这个函数用于打印20000以内的所有素数。

    它通过两层循环来判断每个数是否为素数，外层循环遍历2到19999的所有数，
    内层循环用于判断当前数是否能被2到其平方根之间的数整除，如果不能，则为素数并打印出来。
    """
    for num in range(2, 20000):
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                break
        else:
            print(num)


# B: 计算整数2-10000之间的素数的个数（无参数有返回值）
@time_cost
def count_prime_numbers_1():

    count = 0
    for num in range(2, 10000):
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                break
        else:
            count += 1
    return count

@time_cost
def count_prime_numbers_2(M):

    count = 0
    for num in range(2, M):
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                break
        else:
            count += 1
    return count
